package com.github.jspxnet.worktask.core.node;

import com.github.jspxnet.utils.BeanUtil;
import com.github.jspxnet.utils.ObjectUtil;
import com.github.jspxnet.utils.StringUtil;
import com.github.jspxnet.worktask.model.dto.ProcessResponse;
import com.github.jspxnet.worktask.model.enums.FlowNodeEnumType;
import com.github.jspxnet.worktask.model.enums.WorkTaskEnumType;
import com.github.jspxnet.worktask.table.ConfFlowNode;
import com.github.jspxnet.worktask.table.WorkTask;
import com.github.jspxnet.worktask.util.WorkFlowUtil;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 分支，如何分支通过 WayScript 来控制
 * WayScript
 * 可以通过进入的变量来控制
 * 也可以是进入的参数
 * 例如${auditType==1?'node1':'node2'}
 * 分别标识审核成功和失败
 *
 */
public class ForkNodeAction  extends BaseNodeAction {
    @Override
    public ProcessResponse<?> execute() {

        //分支脚本
        List<ConfFlowNode> nextNodeList = confWorkFlowDAO.getNextList(currentWorkTask.getFlowNo(), currentWorkTask.getNamespace(), currentWorkTask.getNodeId());
        try {
            String wayScript = currentConfFlowNode.getWayScript();
            if (StringUtil.isNull(wayScript))
            {
                for (ConfFlowNode confFlowNode : nextNodeList) {

                    WorkTask workTask = BeanUtil.copy(workTaskParam, WorkTask.class);
                    workTask.setNodeId(confFlowNode.getNodeId());
                    workTask.setCreateDate(new Date());
                    workTask.setFlowNodeType(confFlowNode.getFlowNodeType());
                    workTask.setUsers(workTaskParam.getUsers());
                    workTask.setId(0);
                    runtimeServiceDAO.save(workTask);
                }
            } else
            {
                Map<String, Object>  valueMap = ObjectUtil.getMap(workTaskParam);
                valueMap.putAll(variables);
                String nodeId = WorkFlowUtil.runScript(wayScript,valueMap);
                if (StringUtil.isNull(nodeId))
                {
                    return ProcessResponse.error("脚本生成节点为空");
                }

                for (ConfFlowNode confFlowNode : nextNodeList) {
                    if (confFlowNode.getNodeId().equals(nodeId))
                    {
                        WorkTask workTask = BeanUtil.copy(workTaskParam, WorkTask.class);
                        workTask.setNodeId(confFlowNode.getNodeId());
                        workTask.setCreateDate(new Date());
                        workTask.setFlowNodeType(confFlowNode.getFlowNodeType());
                        workTask.setUsers(workTaskParam.getUsers());
                        workTask.setId(0);
                        runtimeServiceDAO.save(workTask);
                    } else
                    {
                        if (!userAdapterDAO.isAuthority(confFlowNode, workTaskParam.getUserId())) {
                            continue;
                        }
                        WorkTask workTask = BeanUtil.copy(workTaskParam, WorkTask.class);
                        workTask.setNodeId(confFlowNode.getNodeId());
                        workTask.setCreateDate(new Date());
                        workTask.setFlowNodeType(FlowNodeEnumType.END.getValue());
                        workTask.setUsers(workTaskParam.getUsers());
                        workTask.setId(0);
                        runtimeServiceDAO.save(workTask);
                    }
                }
            }


            currentWorkTask.setUsers(workTaskParam.getUsers());
            currentWorkTask.setWorkTaskType(WorkTaskEnumType.NONE.getValue());
            currentWorkTask.setFlowNodeType(FlowNodeEnumType.END.getValue());
            runtimeServiceDAO.update(currentWorkTask,new String[]{"flowNodeType","workTaskType","users"});

        } catch (Exception e)
        {
            e.printStackTrace();
            return ProcessResponse.error(e.getMessage());
        } finally {
            finishNode();
        }
        return ProcessResponse.success(currentWorkTask);
    }
}
